package etllog2;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @program: WCount
 * @description: Mapreduce的Mapper阶段
 * @author: Qiang.Ye
 * @create: 2019-05-23 07:19
 **/
public class LogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {

    Text k = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //1.获取一行数据
        String line = value.toString();
        //2.解析日志
        LogBean logBean = parseLog(line, context);

        //3.判断非法数据
        if(!logBean.isValid()){
            return ;
        }
        k.set(logBean.toString());

        //4.输出
        context.write(k,NullWritable.get());

    }
    /** 
    * @Description:  
    * @Author: Qiang.Ye
    * @Date: 2019/5/23 
    * @param line
     * @param context 
    * @return: etllog2.LogBean 
    */ 
    private LogBean parseLog(String line, Context context) {
        LogBean logBean = new LogBean();
        //1.分割
        String[] filds = line.split(" ");

        //数据封装
        if(filds.length>11){
            logBean.setRemote_addr(filds[0]);
            logBean.setRemote_user(filds[1]);
            logBean.setTime_local(filds[3].substring(1));
            logBean.setRequest(filds[6]);
            logBean.setStatus(filds[8]);
            logBean.setBody_bytes_sent(filds[9]);
            logBean.setBody_bytes_sent(filds[9]);
            logBean.setHttp_referer(filds[10]);
            //若果长度大于12，就拼接浏览器来源
            if (filds.length > 12) {
                logBean.setHttp_user_agent(filds[11] + " " + filds[12]);
            }else {
                logBean.setHttp_user_agent(filds[11]);
            }
            //查看状态码，若大于400则是失败状态，过滤掉
            if(Integer.parseInt(logBean.getStatus()) > 400){
                logBean.setValid(false);
            }

        }else {
            logBean.setValid(false);
        }

        return logBean;
    }
}
